package com.vhly.tuite;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.vhly.tuite.controller.TwitterAsyncTask;
import com.vhly.tuite.controller.TwitterClientDelegate;
import com.vhly.tuite.twclient.client.TwitterClient;
import org.w3c.dom.Element;

/**
 * Created by IntelliJ IDEA.
 * User: vhly
 * Date: 2010-7-28
 * Time: 17:36:52
 *
 * @author vhly[FR]
 */
public class WriteTweetActivity extends Activity implements View.OnClickListener, TwitterClientDelegate {
    public static final int ACTION_WRITE = 0;
    public static final int ACTION_REPLY = 1;

    private EditText editor;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.writetweet);
        Button btnsend = (Button) findViewById(R.id.btnsend);
        if (btnsend != null) {
            btnsend.setOnClickListener(this);
        }
        editor = (EditText) findViewById(R.id.tweditor);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        setContentView(R.layout.writetweet);
        int sz = outState.size();
        Log.d("WriteTweetActivity", "Bundle size is " + sz);
        int type = outState.getInt("tt", ACTION_WRITE);
        if (type == ACTION_WRITE) {
            setTitle(R.string.write);
        } else if (type == ACTION_REPLY) {
            setTitle(R.string.reply);
            EditText txt = (EditText) findViewById(R.id.tweditor);
            String user = outState.getString("tu");
            StringBuffer sb = new StringBuffer();
            sb.append('@').append(user).append(' ');
            txt.setText(sb.toString());
            sb = null;
        }
    }

    public void onClick(View view) {
        if (view instanceof Button) {
            Button btn = (Button) view;
            int id = btn.getId();
            if (id == R.id.btnsend) {
                if (editor != null) {
                    String et = editor.getText().toString();
                    Log.d("WriteTweetActivity", et);
                    TwitterClient client = TwitterClient.getInstance();
                    TwitterAsyncTask task = new TwitterAsyncTask(client, this);
                    task.execute(client.makeStatusesUpdate(et));
                }
            }
        }
    }

    /**
     *
     *
     * @param el Element XML DOM
     */
    public void process(Element el) {
        if (el != null) {
            String n = el.getNodeName();
            if ("status".equals(n)) {
                String ns = Context.NOTIFICATION_SERVICE;
                NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
                int icon = R.drawable.stat_happy;
                CharSequence tickerText = "Update OK";
                long when = System.currentTimeMillis();
                Notification notification = new Notification(icon, tickerText, when);

                Context context = getApplicationContext();
                CharSequence contentTitle = "My notification";
                CharSequence contentText = "Update OK";
                notification.flags = Notification.FLAG_AUTO_CANCEL;
                Intent notificationIntent = new Intent(this, TabMainActivity.class);
                PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
                notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

                mNotificationManager.notify(Constant.NOTIFY_TWEET_UPDATE,notification);
                
            }
        }
    }
}
